package org.aynu.propertyservice.controller;

import org.aynu.propertyservice.common.PropertyResponse;
import org.aynu.propertyservice.domain.PropertyFeesPo;
import org.aynu.propertyservice.service.IPropertyFeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 物业费用控制器
 *
 * @author 小粥
 * @since 2025/03/03
 */
@RestController
@RequestMapping("/property-fees")
public class PropertyFeesController {
    private final IPropertyFeesService propertyFeesService;

    @Autowired
    public PropertyFeesController(IPropertyFeesService propertyFeesService) {
        this.propertyFeesService = propertyFeesService;
    }

    /**
     * 查询居民物业费用清单
     *
     * @param propertyFeesPo 物业费 PO
     * @return {@link PropertyResponse }<{@link List }<{@link PropertyFeesPo }>>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/selectResident", method = RequestMethod.POST)
    public PropertyResponse<List<PropertyFeesPo>> selectResident(@RequestBody PropertyFeesPo propertyFeesPo) {
        return propertyFeesService.selectResident(propertyFeesPo);
    }

    /**
     * 创建费用清单
     *
     * @param propertyFeesPo 物业费 PO
     * @return {@link PropertyResponse }<{@link Boolean }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public PropertyResponse<Boolean> save(@RequestBody PropertyFeesPo propertyFeesPo) {
        return PropertyResponse.success(propertyFeesService.save(propertyFeesPo), "所有用户已存在该年份缴费记录");
    }


    /*创建没有物业缴费订单的所有居民的缴费订单*/
    @RequestMapping(value = "/createAll", method = RequestMethod.POST)
    public PropertyResponse<Integer> createAll(@RequestBody PropertyFeesPo propertyFeesPo) {
        int count = propertyFeesService.createAll(propertyFeesPo);
        if (count == 0) {
            return PropertyResponse.success(count, "所有用户已存在该周期缴费记录");
        }
        return PropertyResponse.success(count, "成功为" + count + "个用户创建缴费订单");
    }


    /**
     * 更新物业费状态
     *
     * @param map 请求参数 (feeId, status, paidAt)
     * @return {@link PropertyResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/05
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public PropertyResponse<Integer> updateStatus(@RequestBody Map<String, Object> map) {
        Integer feeId = Integer.valueOf(map.get("feeId").toString());
        String status = map.get("status").toString();
        Timestamp paidAt = null;

        if (map.containsKey("paidAt")) {
            String paidAtStr = map.get("paidAt").toString();
            try {
                // 使用 ISO_LOCAL_DATE_TIME 解析日期字符串
                DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
                LocalDateTime localDateTime = LocalDateTime.parse(paidAtStr, formatter);
                paidAt = Timestamp.valueOf(localDateTime);
            } catch (Exception e) {
                // 可以根据实际情况进行日志记录或者返回错误响应
                System.err.println("日期解析失败: " + e.getMessage());
                return PropertyResponse.success(0, "所有用户已存在该年份缴费记录");
            }
        }

        boolean updated = propertyFeesService.lambdaUpdate()
                .eq(PropertyFeesPo::getFeeId, feeId)
                .set(PropertyFeesPo::getStatus, status)
                .set(paidAt != null, PropertyFeesPo::getPaidAt, paidAt)
                .update();

        return PropertyResponse.success(updated ? 1 : 0, "所有用户已存在该年份缴费记录");
    }
    /*查询所有物业缴费*/
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public PropertyResponse<List<PropertyFeesPo>> selectAll() {
        List<PropertyFeesPo> propertyFeesPoList = propertyFeesService.list();
        return PropertyResponse.success(propertyFeesPoList, "所有用户已存在该年份缴费记录");
    }

    /*查询未缴费居民*/
    @RequestMapping(value = "/selectUnpaidResident", method = RequestMethod.POST)
    public PropertyResponse<List<PropertyFeesPo>> selectUnpaidResident() {
        List<PropertyFeesPo> propertyFeesPoList = propertyFeesService.lambdaQuery()
                .eq(PropertyFeesPo::getStatus, "未支付")
                .list();
        return PropertyResponse.success(propertyFeesPoList, "所有用户已存在该年份缴费记录");
    }

}